package ciicos.service.impl;

import ciicos.base.service.SuperServiceImpl;
import ciicos.config.ConfigValueUtil;
import ciicos.entity.LineDocumentEntity;
import ciicos.entity.LineDocumentShareEntity;
import ciicos.mapper.LineDocumentMapper;
import ciicos.model.linedocument.PageDocument;
import ciicos.service.LineDocumentService;
import ciicos.service.LineDocumentShareService;
import ciicos.util.FileUtil;
import ciicos.util.RandomUtil;
import ciicos.util.StringUtil;
import ciicos.util.UserProvider;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 知识文档
 *
 * @author 企业研发中心
 * @copyright 中工互联（北京）科技集团有限公司
 * @date 2019年9月26日 上午9:18
 */
@Service
public class LineDocumentServiceImpl extends SuperServiceImpl<LineDocumentMapper, LineDocumentEntity> implements LineDocumentService {

    @Autowired
    private UserProvider userProvider;
    @Autowired
    private LineDocumentShareService documentShareService;
    @Autowired
    private ConfigValueUtil configValueUtil;

    @Override
    public List<LineDocumentEntity> getFolderList() {
        QueryWrapper<LineDocumentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(LineDocumentEntity::getCreatorUserId, userProvider.get().getUserId())
                .eq(LineDocumentEntity::getType, 0)
                .eq(LineDocumentEntity::getEnabledMark, 1)
                .orderByAsc(LineDocumentEntity::getCreatorTime);
        return this.list(queryWrapper);
    }

    @Override
    public List<LineDocumentEntity> getAllList(String parentId) {
        QueryWrapper<LineDocumentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(LineDocumentEntity::getCreatorUserId, userProvider.get().getUserId())
                .eq(LineDocumentEntity::getEnabledMark, 1)
                .eq(LineDocumentEntity::getParentId, parentId)
                .orderByDesc(LineDocumentEntity::getCreatorTime);
        return this.list(queryWrapper);
    }

    @Override
    public List<LineDocumentEntity> getAllList(PageDocument page) {
        QueryWrapper<LineDocumentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(LineDocumentEntity::getCreatorUserId, userProvider.get().getUserId())
                .eq(LineDocumentEntity::getEnabledMark, 1)
                .eq(LineDocumentEntity::getParentId, page.getParentId())
                .orderByAsc(LineDocumentEntity::getType)
                .orderByAsc(LineDocumentEntity::getCreatorTime);
        if (!StringUtil.isEmpty(page.getKeyword())) {
            queryWrapper.lambda().like(LineDocumentEntity::getFullName, page.getKeyword());
        }
        return this.list(queryWrapper);
    }

    @Override
    public List<LineDocumentEntity> getTrashList() {
        QueryWrapper<LineDocumentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(LineDocumentEntity::getCreatorUserId, userProvider.get().getUserId())
                .eq(LineDocumentEntity::getEnabledMark, 0)
                .orderByDesc(LineDocumentEntity::getDeleteTime);
        return this.list(queryWrapper);
    }

    @Override
    public List<LineDocumentEntity> getShareOutList() {
        QueryWrapper<LineDocumentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(LineDocumentEntity::getCreatorUserId, userProvider.get().getUserId())
                .eq(LineDocumentEntity::getEnabledMark, 1)
                .gt(LineDocumentEntity::getIsShare, 0)
                .orderByAsc(LineDocumentEntity::getType)
                .orderByDesc(LineDocumentEntity::getDeleteTime);
        return this.list(queryWrapper);
    }

    @Override
    public List<LineDocumentEntity> getShareTomeList() {
        return this.baseMapper.getShareTomeList(userProvider.get().getUserId());
    }

    @Override
    public List<LineDocumentShareEntity> getShareUserList(String documentId) {
        QueryWrapper<LineDocumentShareEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(LineDocumentShareEntity::getDocumentId, documentId);
        return documentShareService.list(queryWrapper);
    }

    @Override
    public LineDocumentEntity getInfo(String id) {
        QueryWrapper<LineDocumentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(LineDocumentEntity::getId, id);
        return this.getOne(queryWrapper);
    }

    @Override
    public void delete(LineDocumentEntity entity) {
        entity.setDeleteTime(new Date());
        entity.setDeleteUserId(userProvider.get().getUserId());
        entity.setEnabledMark(0);
        this.updateById(entity);
    }

    @Override
    public void create(LineDocumentEntity entity) {
        entity.setId(RandomUtil.uuId());
        entity.setCreatorUserId(userProvider.get().getUserId());
        entity.setEnabledMark(1);
        this.save(entity);
    }

    @Override
    public boolean update(String id, LineDocumentEntity entity) {
        entity.setId(id);
        entity.setLastModifyTime(new Date());
        entity.setLastModifyUserId(userProvider.get().getUserId());
        return this.updateById(entity);
    }

    @Override
    @DSTransactional
    public boolean shareCreate(String documentId, String[] shareUserId) {
        List<LineDocumentShareEntity> entitys = new ArrayList<>();
        for (String item : shareUserId) {
            LineDocumentShareEntity entity = new LineDocumentShareEntity();
            entity.setId(RandomUtil.uuId());
            entity.setDocumentId(documentId);
            entity.setShareUserId(item);
            entity.setShareTime(new Date());
            entitys.add(entity);
        }
        QueryWrapper<LineDocumentEntity> queryWrapper = new QueryWrapper<>();
        LineDocumentEntity entity = this.getOne(queryWrapper.lambda().eq(LineDocumentEntity::getId, documentId));
        if (entity != null) {
            entity.setIsShare(entitys.size());
            entity.setShareTime(new Date());
            this.updateById(entity);
            QueryWrapper<LineDocumentShareEntity> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(LineDocumentShareEntity::getDocumentId, documentId);
            documentShareService.remove(wrapper);
            for (LineDocumentShareEntity shareEntity : entitys) {
                documentShareService.save(shareEntity);
            }
            return true;
        }
        return false;
    }

    @Override
    @DSTransactional
    public boolean shareCancel(String documentId) {
        QueryWrapper<LineDocumentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(LineDocumentEntity::getId, documentId);
        LineDocumentEntity entity = this.getOne(queryWrapper);
        if (entity != null) {
            entity.setIsShare(0);
            entity.setShareTime(new Date());
            this.updateById(entity);
            QueryWrapper<LineDocumentShareEntity> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(LineDocumentShareEntity::getDocumentId, documentId);
            documentShareService.remove(wrapper);
            return true;
        }
        return false;
    }

    @Override
    @DSTransactional
    public void trashDelete(String folderId) {
        LineDocumentEntity entity = this.getInfo(folderId);
        if(entity!=null){
            this.removeById(folderId);
            FileUtil.deleteFile(configValueUtil.getDocumentFilePath() + entity.getFilePath());
        }
//        List<DocumentEntity> list = this.baseMapper.GetChildList(folderId);
//        List<String> deleteId = new ArrayList<>();
//        for (DocumentEntity entity : list) {
//            if(!StringUtil.isEmpty(entity.getFilePath())){
//                FileUtil.deleteFile(configValueUtil.getDocumentFilePath() + entity.getFilePath());
//            }
//            deleteId.add(entity.getId());
//        }
//        this.removeByIds(deleteId);
    }

    @Override
    public boolean trashRecovery(String id) {
       return retBool(this.baseMapper.trashRecovery(id));
    }

    @Override
    public boolean moveTo(String id, String toId) {
        LineDocumentEntity entity = this.getInfo(id);
        if(entity!=null){
            entity.setParentId(toId);
            this.updateById(entity);
           return true;
        }
       return false;
    }

    @Override
    public boolean isExistByFullName(String fullName, String id, String parentId) {
        String userId=userProvider.get().getUserId();
        QueryWrapper<LineDocumentEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(LineDocumentEntity::getFullName,fullName).eq(LineDocumentEntity::getEnabledMark, 1).eq(LineDocumentEntity::getCreatorUserId,userId);
        queryWrapper.lambda().eq(LineDocumentEntity::getParentId, parentId);
        if(!StringUtil.isEmpty(id)){
            queryWrapper.lambda().ne(LineDocumentEntity::getId, id);
        }
        return this.count(queryWrapper) > 0 ? true : false;
    }
}
